/*******************************************************************************
 * 文件名: Common.java
 * 描    述: 公共类
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2017-12-22
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core.utils;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;

import com.z1.core.exception.Z1Exception;
import com.z1.core.sequence.SequenceUtil;

public class Utils implements UtilsConst {
	
	private static Logger logger = Logger.getLogger(Utils.class);
	
	static {
		if (null == System.getenv("APP_HOME")) {
			System.err.println("APP_HOME not found...");
		}
	}
	
	/**
	 * 获取应用程序下目录
	 * @param dirName
	 * @return
	 */
	public static final File getConfigPath() {
  		return getDirOfApp(CONFIG_DIRNAME);
  	}
	
	/**
	 * 获取应用程序下目录
	 * @param dirName
	 * @return
	 */
	public static final File getDirOfApp(String dirName) {
  		if (null != dirName) {
  			File dir = new File(new StringBuffer(APP_HOME)
  				.append(File.separator)
  				.append(dirName)
  				.toString()
  			);
  			if (dir.exists() && dir.isDirectory()) { }
  	    	else { dir.mkdirs(); }
  			return dir;
  		}
  		return null;
  	}
	
	/**
	 * 获取数据导入临时存储目录
	 * @param suffix
	 * @return
	 */
	public static final File getImportFileLocalFile(String suffix) {
		File tmpDir = getDirOfApp(IMPORT_FILE_TEMP_DIRNAME);
    	if (null == tmpDir) {
    		throw new Z1Exception("tmp dir is null.");
    	}
        return new File(tmpDir, SequenceUtil.generateUUID() + "." + suffix);
	}
	
	/**
	 * 获取默认头像图片
	 * @return
	 */
	public static final byte[] getDefaultHeadPic() {
		byte[] buf = null;
		InputStream input = null;
		try {
			input = Utils.class.getResourceAsStream("/com/team/quickerp/util/headpic/default_user.jpg");
			if (null != input) {
				int len = input.available();
				buf = new byte[len];
				input.read(buf);
			}
		}
		catch (Throwable e) {
			logger.error("get default user pic err", e);
		}
		finally {
			if (null != input) {
				try { input.close(); }
				catch (Exception e) { }
				input = null;
			}
		}
		return buf;
	}
	
	/**
	 * 获取默认客户纠错图片
	 * @return
	 */
	public static final byte[] getDefaultCustomerCorrectPic() {
		byte[] buf = null;
		InputStream input = null;
		try {
			input = Utils.class.getResourceAsStream("/com/team/quickerp/util/customer/correct_def.jpg");
			if (null != input) {
				int len = input.available();
				buf = new byte[len];
				input.read(buf);
			}
		}
		catch (Throwable e) {
			logger.error("get default customer correct pic err", e);
		}
		finally {
			if (null != input) {
				try { input.close(); }
				catch (Exception e) { }
				input = null;
			}
		}
		return buf;
	}
	
}
